package com.edu.hre.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;



import com.edu.hre.db.config.BaseDAO;
import com.edu.hre.db.config.DBPool;
import com.edu.hre.db.dto.HreEmpEmergencyContact;


public class EmpEmergencyContactDAO  extends BaseDAO {
	private static final Logger logger = Logger.getLogger(EmpEmergencyContactDAO.class);
	public List<HreEmpEmergencyContact> getEmergencyContactByEmployeeId(
			int employeeNumber) {
		List<HreEmpEmergencyContact> listEmergencyContacts = new ArrayList<HreEmpEmergencyContact>();
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		try {
			conn = DBPool.getConnection();
			String strSQLQuery = "SELECT * FROM hre_emp_emergency_contacts WHERE emp_number = "
					+ employeeNumber;
			preStmt = conn.prepareStatement(strSQLQuery);
			rs = preStmt.executeQuery();
			while (rs.next()) {
				HreEmpEmergencyContact emergencyContact = new HreEmpEmergencyContact();
				emergencyContact.setEcName(rs.getString("eec_name"));
				emergencyContact.setEcRelationship(rs.getString("eec_relationship"));
				emergencyContact.setEcHomeNo(rs.getString("eec_home_no"));
				emergencyContact.setEcMobileNo(rs.getString("eec_mobile_no"));
				emergencyContact.setEcOfficeNo(rs.getString("eec_office_no"));
				//.....
				//.....
				listEmergencyContacts.add(emergencyContact);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// đóng PreparedStatement, ResultSet và đẩy connection vào pool
			releaseConnection(conn, preStmt, rs);
		}
		return listEmergencyContacts;
	}

}
